//
//  CountDownLabel.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/11.
//

import Foundation
import MYModule

public class CountDownLabel: Label{
    
    public enum CountDownType{
        case countDownBig
        case countDownSmall
    }
    
    public typealias CountDownSuccessBlock = (() -> Void)
    public typealias CountDownBeginBlock = (() -> Void)
    
    public var number: Int!
    public var endTitle: String!
    public var countDownSuccessBlock: CountDownSuccessBlock!
    public var CountDownBeginBlock: CountDownBeginBlock!
    public var isAnimationing: Bool = false
    public var scale:CGFloat = 0.5
    public var textFont = Fonts.systemFont(ofSize: 30, weight: .regular)
    public var label_borderColor = UIColor.white
    public var label_borderWidth: CGFloat = 0
    public var label_maskToBounds = true
    public var countDownType = CountDownType.countDownSmall
    
    public func hidden(){
        self.isAnimationing = false
        self.transform = CGAffineTransform.identity
        self.isHidden = true
    }
    
    public func play(number: Int, endTitle: String?, success: CountDownSuccessBlock?){
        self.isHidden = false
        self.number = number
        if endTitle != nil {
            self.endTitle = endTitle
        }
        if success != nil {
            self.countDownSuccessBlock = success
            setLabelBase()
            scaleAction(success: success)
        }
        
    }
    
    
    
    public func scaleAction(success: CountDownSuccessBlock?){
        log.debug("number = \(self.number)")
        if self.number >= ((self.endTitle != nil) ? 0 : 1){
            self.text = self.number == 0 ? self.endTitle : String(self.number)
            UIView.animate(withDuration: 1, animations: {
                self.transform = CGAffineTransform.identity
                if self.countDownType == .countDownSmall{
                    self.alpha = 0
                }else if self.countDownType == .countDownBig{
                    self.alpha = 1
                }
            }) { (finish) in
                log.debug("finish=\(finish)")
                self.number -= 1
                if self.countDownType == .countDownSmall{
                    self.alpha = 1
                }else if self.countDownType == .countDownBig{
                    self.alpha = 0
                }
                self.transform = CGAffineTransform.init(scaleX: self.scale, y: self.scale)
                self.scaleAction(success: success)
                
            }
        }else{
            if success != nil {
                success!()
            }
        }
    }
    
    public func setLabelBase(){
        self.transform = CGAffineTransform.init(scaleX: scale, y: scale)
        if self.countDownType == .countDownSmall{
            self.alpha = 1
        }else if self.countDownType == .countDownBig {
            self.alpha = 0
        }
        
        self.text = "\(self.number)"
        self.textColor = UIColor.white
        self.font = textFont
        self.center = CGPoint(x: self.bounds.width / 2, y: self.bounds.height / 2)
        self.textAlignment = NSTextAlignment.center
        self.layer.cornerRadius = self.bounds.width / 2
        self.layer.borderColor = label_borderColor.cgColor
        self.layer.borderWidth = label_borderWidth
        self.layer.masksToBounds = label_maskToBounds
    }
}

public func setShadow(view:UIView?,sColor:UIColor = .black,offset:CGSize = CGSize(width: 2, height: 2),opacity:Float = 0.2,radius:CGFloat = 5, cornerRadius: CGFloat = 10) {
    view?.layer.shadowColor = sColor.cgColor
    //设置透明度
    view?.layer.shadowOpacity = opacity
    //设置阴影半径
    view?.layer.shadowRadius = radius
    //设置阴影偏移量
    view?.layer.shadowOffset = offset
    //设置圆角
    view?.layer.cornerRadius = cornerRadius
}
